package com.hapedu.gegeric.Homework;


import org.junit.jupiter.api.Test;

import java.util.*;

public class Homework01 {
    public static void main(String[] args) {

    }
    @Test
    public void testList(){
        DAO<User> dao = new DAO<>();
        dao.save("001",new User(10,"jack"));
        dao.save("002",new User(18,"king"));
        dao.save("003",new User(38,"smith"));
        List<User> list = dao.list();
        System.out.println("list"+list);
        dao.update("003",new User(49,"milan"));
        System.out.println("==================");
        list = dao.list();
        System.out.println("list"+list);
    }
}

class DAO<T>{
    private Map<String,T> map = new HashMap<>();
    public T get(String id){
        return map.get(id);
    }
    public void update(String id,T entity){
        map.put(id,entity);
    }
    public List<T> list(){
        ArrayList<T> ts = new ArrayList<>();

        Set<String> keyset = map.keySet();
        for (String key : keyset) {
            ts.add(map.get(key));
        }
        return ts;
    }

    public void delete(String id){
        map.remove(id);
    }

    public void save(String id,T entity){
        map.put(id,entity);
    }
}

class User{
    private String name;
    private int age;

    public User(int age,String name) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
